package com.sise.wangzhan;

import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname StuController
 * @Description TODO
 * @Date 2020/4/9 9:25
 * @Created by wangzhan
 */
@RestController
public class StuController {

    //consumes表示请求的格式，需要进行编码器
    @RequestMapping(value = "/stu/sum",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public String getSum(@RequestBody Stu stu){
        int sum = stu.getChinese()+stu.getMath()+stu.getEnglish();

        return stu.getSname()+"学生的总成绩为："+sum;
    }

    //默认是 以JSON格式响应
    //produces表示响应的格式，需要进行解码器
    @RequestMapping(value = "/stu/{sid}", method = RequestMethod.GET/*, produces = MediaType.APPLICATION_JSON_VALUE*/)
    public Object getStu(@PathVariable("sid") Integer sid){
        Stu stu = new Stu(sid,"TOMMY",89,80,75);

        Map map = new HashMap();
        map.put("id",stu.getSid());
        map.put("姓名",stu.getSname());
        map.put("语文",stu.getChinese());
        map.put("数学",stu.getMath());
        map.put("英语",stu.getEnglish());

        //String s = JSON.toJSONString(map);
        Object json = JSON.toJSON(map);

        return json;
    }

    //以XML格式返回
    @RequestMapping(value = "/stu/createstu", method = RequestMethod.POST,
                    consumes = MediaType.APPLICATION_XML_VALUE,
                    produces = MediaType.APPLICATION_XML_VALUE)
    public String createStu(@RequestBody Stu stu){

        double average = (stu.getChinese()+stu.getMath()+stu.getEnglish()) / 3;

        return "<result><message>平均成绩："+average+"</message></result>";
    }
}
